<?php require_once(CONTROLLER_PATH . 'admin/Admin_master_controller.php');
class Category extends Admin_master_controller {
	protected $_page_uri = "category";
	
	function __construct() {
		parent::__construct();
		$this->load->model('DNN_model');
		$this->load->model('Category_model');
		$this->_page_uri = admin_url() . $this->_page_uri;
		$this->view_data['page_uri'] = $this->_page_uri;
		$this->view_data['active_category'] = "current_page_item";
		$this->view_data['moduleTitle'] = $this->lang->line('category-manager');

		// get parent categories
		$this->view_data['categories'] = $this->Category_model->get_list_parent()->result();
	}
	function index($offset = 0) {
		$this->view_data['view'] = ADMIN_PATH . "category_list";
		$this->view_data['page_title'] = $this->lang->line('category-list');
		$this->view_data['page_title_class'] = 'header icon-48-config';
		$data_list = $this->Category_model->get_paged_list(ADMIN_ITEM_PER_PAGE, $offset)->result();
		$this->view_data['pagination'] = $this->_render_paging($this->Category_model->count_all(), $offset);
		$this->view_data['table'] = $this->_render_table($data_list, $offset);
		$this->view_data['offset'] = $offset;
		$this->render_page($this->view_data);
	}
	
	//Private Functions
	function _render_table($data_list, $offset = 0) {
		// generate table data
		$this->load->library('table');
		$this->table->set_empty("&nbsp;");
		$this->table->set_heading($this->lang->line('no'), $this->lang->line('name'), $this->lang->line('type'), $this->lang->line('parent-id'), $this->lang->line('actions'));
		$i = $offset;
		foreach ($data_list as $data_item) {
			$this->table->add_row(++$i, $data_item->name, $this->to_type($data_item->type), $data_item->parent_id,
				anchor($this->_page_uri.'/edit/'.$data_item->id . '/' . $offset ,$this->lang->line('edit') ,array('class'=>'update')).' '.
				anchor($this->_page_uri.'/delete/'.$data_item->id . '/' . $offset ,$this->lang->line('delete') ,array('class'=>'delete','onclick'=>"return confirm('".$this->lang->line('delete-confirm')."');"))
			);
		}
		return $this->table->generate();
	}
	
	function to_type($type) {
		return $type == 1 ? $this->lang->line('type-product') : $this->lang->line('type-news');
	}
	function add($offset = 0) {
		// set common properties
		$this->view_data['view'] = ADMIN_PATH . "category_edit";
		$this->view_data['page_title'] = $this->lang->line('category-add');
		$this->view_data['page_title_class'] = 'header icon-48-config';
		$this->view_data['action'] = $this->_page_uri . '/add/' . $offset;
		$this->view_data['link_back'] = $this->_page_uri . '/index/' . $offset;
		// set validation properties
		$this->_set_fields();
		$this->_set_rules();
		// run validation
		if ($this->validation->run() == FALSE) {
			$this->view_data['message'] = '';
		} else {
			$data_arr = array (
				'name_'.$this->_language => $this->input->post('name'),
				'type' => $this->input->post('type'),
				'parent_id' => $this->input->post('parent_id'),
			);
			$id = $this->Category_model->add($data_arr);
			$this->validation->id = $id;
			$this->view_data['message'] = $this->lang->line('add-success');
		}
		$this->render_page($this->view_data);
	}
	
	function edit($id = 0, $offset) {
		//set common properties
		$this->view_data['view'] = ADMIN_PATH . "category_edit";
		$this->view_data['page_title'] = $this->lang->line('category-edit');
		$this->view_data['page_title_class'] = 'header icon-48-config';
		$this->view_data['action'] = $this->_page_uri . '/edit/' . $id . "/" . $offset;
		$this->view_data['link_back'] = $this->_page_uri . '/index/' . $offset;
		
		//set validation properties
		$this->_set_fields();
		$this->_set_rules();
		//run validation
		if ($this->validation->run() == FALSE) {
			$this->view_data['message'] = '';
		} else {
			//save data
			$id = $this->input->post('id');
			$data_arr = array (
				'name_'.$this->_language => $this->input->post('name'),
				'type' => $this->input->post('type'),
				'parent_id' => $this->input->post('parent_id'),
			);
			$this->Category_model->update($id, $data_arr);
			$this->view_data['message'] = $this->lang->line('edit-success');
		}
		//prefill form values
		$data_arr = $this->Category_model->get_by_id($id)->row();
		$this->validation->id = $id;
		$this->validation->name = $data_arr->name;
		$this->validation->type = $data_arr->type;
		$_POST['parent_id'] = $data_arr->parent_id;
		
		$this->render_page($this->view_data);
	}
	
	function delete($id = 0, $offset = 0) {
		$this->Category_model->delete($id);
		redirect($this->_page_uri.'/index/' . $offset,'refresh');
	}
	// validation fields
	function _set_fields() {
		$fields['id'] = 'id';
		$fields['name'] = 'name';
		$fields['type'] = 'type';
		$fields['parent_id'] = 'parent_id';
		$this->validation->set_fields($fields);
	}
	//validation rules
	function _set_rules() {
		$rules['id'] = 'trim';
		$rules['name'] = 'trim|required';
		$rules['type'] = 'trim|required';
		$rules['parent_id'] = 'trim';
		$this->validation->set_rules($rules);
		$this->validation->set_message('required', $this->lang->line('required'));
		$this->validation->set_message('isset', $this->lang->line('isset'));
		$this->validation->set_error_delimiters('<p class="error">', '</p>');
	}
}
